package com.sinosoft.perfect.system._authorization.dao;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseSqlSessionDaoSupport;
import com.sinosoft.perfect.system._.po.TUserRolebusinessRelation;
import com.sinosoft.perfect.system._authorization.MapperSystemAuthorization;

public class UserRolebusinessAuthorizeDaoImpl extends BaseSqlSessionDaoSupport implements IUserRolebusinessAuthorizeDao {
	
	private MapperSystemAuthorization getMapperSystemAuthorization(){
		return getSqlSession().getMapper(MapperSystemAuthorization.class);
	}
	
	@Override
	public int deleteRolebusinessRelationsByUser(String userId) throws SQLException {
		return getMapperSystemAuthorization().deleteRolebusinessRelationsByUser(userId);
	}
	
	@Override
	public int deleteRolebusinessRelationsByUsersOfUnit(String unitId) throws SQLException {
		return getMapperSystemAuthorization().deleteRolebusinessRelationsByUsersOfUnit(unitId);
	}
	
	@Override
	public void insertByTUserRolebusinessRelation(TUserRolebusinessRelation tuserRolebusinessRelation) throws SQLException {
		getMapperSystemAuthorization().insertByTUserRolebusinessRelation(tuserRolebusinessRelation);
	}
	
	@Override
	public List<TUserRolebusinessRelation> queryRolebusinessOfUser(String userId) throws SQLException {
		return getMapperSystemAuthorization().queryRolebusinessOfUser(userId);
	}
	
	@Override
	public List<String> queryUserIdsOfUnit(String unitId) throws SQLException {
		return getMapperSystemAuthorization().queryUserIdsOfUnit(unitId);
	}
	
	@Override
	public void delAllRolesOfUserAndAddUserRoles(String userId, List<TUserRolebusinessRelation> tuserRolebusinessRelations) throws SQLException {
		getMapperSystemAuthorization().deleteRolebusinessRelationsByUser(userId);
		if (tuserRolebusinessRelations != null) {
			for (int i = 0; i < tuserRolebusinessRelations.size(); i++) {
				TUserRolebusinessRelation tuserRolebusinessRelation = tuserRolebusinessRelations.get(i);
				if (tuserRolebusinessRelation.getRoleBusinessId() != null && !"".equals(tuserRolebusinessRelation.getRoleBusinessId() + ""))
					getMapperSystemAuthorization().insertByTUserRolebusinessRelation(tuserRolebusinessRelation);
			}
		}
	}
	
	@Override
	public void delAllRoleUsersOfUnitAndAddUsersRoleOfUnit(String unitId, List<TUserRolebusinessRelation> tuserRolebusinessRelations) throws SQLException {
		getMapperSystemAuthorization().deleteRolebusinessRelationsByUsersOfUnit(unitId);
		if (tuserRolebusinessRelations != null) {
			for (int i = 0; i < tuserRolebusinessRelations.size(); i++) {
				TUserRolebusinessRelation tuserRolebusinessRelation = tuserRolebusinessRelations.get(i);
				if (tuserRolebusinessRelation.getRoleBusinessId() != null && !"".equals(tuserRolebusinessRelation.getRoleBusinessId() + ""))
					getMapperSystemAuthorization().insertByTUserRolebusinessRelation(tuserRolebusinessRelation);
			}
		}
			
	}

	@Override
	public void deleteTUserRolebusinessRelation(TUserRolebusinessRelation tUserRolebusinessRelation) throws SQLException {
		getMapperSystemAuthorization().deleteTUserRolebusinessRelation(tUserRolebusinessRelation);
	}
	
}
